package com.taptech.doufu.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.NovelCreateServices;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;

/**
 * Created by op0509 on 2016/5/17.
 */
public class NovelPublishDialog extends Dialog implements android.view.View.OnClickListener, HttpResponseListener {
    private Context context;
    private TextView tvZhuzhan, tvDujia;
    private ImageView imgZhuzhan, imgDujia, imgHelper;
    private RelativeLayout rlZhuzhan, rlDujia;
    private Button btnCancel, btnSure;
    private MineNovelBean bean;
    private int typePublish, from;
    private String name, novelType;
    private PublishTypeSelectListener listener;
    private WaitDialog waitDialog;

    public interface PublishTypeSelectListener{
        void selectListener(String typeName, int typeNum);
    }

    public void setOnPublishTypeSeleceListener(PublishTypeSelectListener listener){
        this.listener = listener;
    }

    public NovelPublishDialog(Context context) {
        super(context);
        this.context = context;
    }
    public NovelPublishDialog(Context context, int theme, int typePublish, MineNovelBean bean, int from) {
        super(context, theme);
        this.context = context;
        this.typePublish = typePublish;
        this.bean = bean;
        this.from = from;// 从哪个页面过来的(目前只做了从小说设置页面的区分)    3为小说设置，其他均为默认
        waitDialog = new WaitDialog(context, R.style.updateDialog, "处理中,请稍后...");
        if(this.bean != null){
            novelType = bean.getNovel_type();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (from == 3) {
            setContentView(R.layout.dialog_novel_create_publish_change);
        }else {
            setContentView(R.layout.dialog_novel_create_publish);
        }


        initUI();
        selectPublish(typePublish);
    }

    private void initUI() {
        tvZhuzhan = (TextView) findViewById(R.id.tv_dialog_novel_create_publish_zhuzhan);
        tvDujia = (TextView) findViewById(R.id.tv_dialog_novel_create_publish_dujia);
        imgZhuzhan = (ImageView) findViewById(R.id.img_dialog_novel_create_publish_zhuzhan);
        imgDujia = (ImageView) findViewById(R.id.img_dialog_novel_create_publish_dujia);

        rlZhuzhan = (RelativeLayout) findViewById(R.id.rl_dialog_novel_create_publish_zhuzhan);
        rlZhuzhan.setOnClickListener(this);
        rlDujia = (RelativeLayout) findViewById(R.id.rl_dialog_novel_create_publish_dujia);
        rlDujia.setOnClickListener(this);

        btnCancel = (Button) findViewById(R.id.btn_dialog_novel_create_publish_cancel);
        btnCancel.setOnClickListener(this);
        btnSure = (Button) findViewById(R.id.btn_dialog_novel_create_publish_sure);
        btnSure.setOnClickListener(this);

        imgHelper = (ImageView) findViewById(R.id.img_dialog_novel_create_publish_help);
        imgHelper.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_dialog_novel_create_publish_zhuzhan:
                selectPublish(1);
                break;
            case R.id.rl_dialog_novel_create_publish_shoufa:
                selectPublish(2);
                break;
            case R.id.rl_dialog_novel_create_publish_dujia:
                selectPublish(3);
                break;
            case R.id.btn_dialog_novel_create_publish_cancel:
                dismiss();
                break;
            case R.id.btn_dialog_novel_create_publish_sure:
                if(1 == typePublish){
                    name = "驻站";
                }else if(2 == typePublish){
                    name = "首发";
                }else if(3 == typePublish){
                    name = "独家";
                }else{
                    UIUtil.toastMessage(context, "请选择发表方式");
                    return;
                }
                if(3 == from){
                    if(waitDialog != null && !waitDialog.isShowing()){
                        waitDialog.show();
                    }
                    if(bean!=null){
                        NovelCreateServices.getInstance().saveTopicNovel(this, bean.getId(), novelType, typePublish+"", null, null, null, null, null);
                    }
                }else {
                    listener.selectListener(name, typePublish);
                    dismiss();
                }
                break;
            case R.id.img_dialog_novel_create_publish_help:
                BrowseActivity.startActivity(context, "http://api.doufu.la/index.php/journal/detail?id=63");
                break;
            default:
                break;
        }
    }

    private void selectPublish(int type){
        tvZhuzhan.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        tvDujia.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        imgZhuzhan.setVisibility(View.INVISIBLE);
        imgDujia.setVisibility(View.INVISIBLE);
        typePublish = type;
        switch (type){
            case 1:
                tvZhuzhan.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgZhuzhan.setVisibility(View.VISIBLE);
                break;
            case 3:
                tvDujia.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgDujia.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        if(waitDialog != null && waitDialog.isShowing()){
            waitDialog.dismiss();
        }
        try {
            if(response.getStatus() == Constant.STATUS_SUCCESS){
                UIUtil.toastMessage(context, "修改成功");
                listener.selectListener(name, typePublish);
                dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
